<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxml="urn:schemas-microsoft-com:xslt"
	xmlns:umbraco.library="urn:umbraco.library"
	xmlns:tagsLib="urn:tagsLib"
	xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
	xmlns:BlogLibrary="urn:BlogLibrary"
  xmlns:Blog="urn:Blog"
	exclude-result-prefixes="msxml umbraco.library tagsLib Exslt.ExsltStrings BlogLibrary Blog">

  <xsl:output method="html" omit-xml-declaration="yes"/>

  <xsl:param name="currentPage"/>
  <!-- Macro Parameters -->
  <!-- Numeric - set the page size for displaying results.  Defaults to 10 -->
  <xsl:variable name="mPageSize" select="/macro/pageSize"/>
  <!-- Boolean - turn the Filter title on or off. Defaults to true -->
  <xsl:variable name="mShowFilterTitle" select="/macro/showFilterTitle"/>
  <!-- Boolean - turn the navigation bars on or off. Defaults to true -->
  <xsl:variable name="mShowNavigationBars" select="/macro/showNavigationBars"/>

  <xsl:variable name="currentBlog" select="Blog:XmlCurrent(number($currentPage/@id))" />

  <xsl:variable name="pageSize">
    <xsl:choose>
      <xsl:when test="$mPageSize &lt;= 0 or string($mPageSize) = '' or string($mPageSize) = 'NaN'">10</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$mPageSize"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="showFilterTitle">
    <xsl:choose>
      <xsl:when test="$mShowFilterTitle = ''">1</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$mShowFilterTitle"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="showNavigationBars">
    <xsl:choose>
      <xsl:when test="$mShowNavigationBars = ''">1</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$mShowNavigationBars"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="pageNumber">
    <xsl:choose>
      <xsl:when test="umbraco.library:RequestQueryString('page') &lt;= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">1</xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="filter">
    <xsl:choose>
      <xsl:when test="string-length(umbraco.library:Request('filterby')) &gt; 0">
        <xsl:value-of select="umbraco.library:Request('filterby')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="''"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="numberOfRecords">
    <xsl:choose>
      <xsl:when test="$filter = ''">
        <xsl:value-of select="count($currentPage//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost',@id)])"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="count($currentPage//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost',@id) and contains(Exslt.ExsltStrings:lowercase(./* [@alias='tags' or name()='tags']), Exslt.ExsltStrings:lowercase($filter))])"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template match="/">
    <section id="posts">
      <!-- Display Title if wanted  - check for tag filter and then Date folder. -->
      <header>
        <xsl:if test="$filter != '' and $showFilterTitle = '1'">
          <h2 class="page-title">
            Archive for tag: <span>
              <xsl:value-of select="$filter"/>
            </span>
          </h2>
        </xsl:if>

        <xsl:if test="BlogLibrary:IsType('DateFolder',$currentPage/@id) and $showFilterTitle = '1'">
          <h2 class="page-title">
            Monthly Archives: <xsl:value-of select="umbraco.library:FormatDateTime(concat($currentPage/../@nodeName,'-',$currentPage/@nodeName,'-1'),'MMMM yyyy')"/>
          </h2>
        </xsl:if>

        <!-- Upper Navigation Bar -->
        <xsl:if test="$numberOfRecords &gt; $pageSize and $showNavigationBars = '1'">
          <xsl:call-template name="navBar">
            <xsl:with-param name="position" select="'above'"/>
          </xsl:call-template>
        </xsl:if>

      </header>

      <!-- If no filter, then display all records -->
      <xsl:if test="$filter = ''">
        <xsl:for-each select="$currentPage//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost',@id)]">
          <xsl:sort select="./* [@alias='postDate' or name()='postDate']" order="descending" />
          <xsl:if test="position() &gt; $pageSize * (number($pageNumber)-1) and position() &lt;= number($pageSize * (number($pageNumber)-1) + $pageSize)">
            <xsl:call-template name="showpost">
              <xsl:with-param name="post" select="."/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>

      <!-- Else display records filtered on tags -->
      <xsl:if test="$filter != ''">
        <xsl:for-each select="$currentPage//*[(@isDoc or name()='node') and BlogLibrary:IsType('BlogPost', @id) and contains(Exslt.ExsltStrings:lowercase(./* [@alias='tags' or name()='tags']), Exslt.ExsltStrings:lowercase($filter))]">
          <xsl:sort select="./* [@alias='postDate' or name()='postDate']" order="descending" />
          <xsl:if test="position() &gt; $pageSize * (number($pageNumber)-1) and position() &lt;= number($pageSize * (number($pageNumber)-1) + $pageSize)">
            <xsl:call-template name="showpost">
              <xsl:with-param name="post" select="."/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
      <footer>
        <!-- Lower Navigation Bar -->
        <xsl:if test="$numberOfRecords &gt; $pageSize and $showNavigationBars = '1'">
          <xsl:call-template name="navBar">
            <xsl:with-param name="position" select="'below'"/>
          </xsl:call-template>
        </xsl:if>
      </footer>
    </section>
  </xsl:template>

  <xsl:template name="showpost">
    <xsl:param name="post"/>
    <article id="post-{$post/@id}" class="post author-{$post/@writerName} {umbraco.library:FormatDateTime($post/PostDate, '\yyyyy \mMM \ddd \hhh')}">
      <header>
        <h3>
          <a href="{umbraco.library:NiceUrl($post/@id)}" title="Permalink to {$post/@nodeName}">
            <xsl:value-of select="$post/@nodeName"/>
          </a>
        </h3>

        <time class="published" datetime="{$post/* [@alias='postDate' or name()='postDate']}">
          <xsl:value-of select="umbraco.library:LongDate($post/* [@alias='postDate' or name()='postDate'])"/>
        </time>
      </header>
      <section class="content">
        <xsl:value-of select="$post/* [@alias='bodyText' or name()='bodyText']" disable-output-escaping="yes"/>
      </section>
      <footer>
        By: <span class="author vcard">
          <span class="fn n">
            <xsl:value-of select="$post/@creatorName"/>
          </span>
        </span>
        <span class="meta-sep"> | </span>
        <span class="tag-links">
          <xsl:variable name="tags" select="tagsLib:getTagsFromNode(@id)" />
          <xsl:choose>
            <xsl:when test="count($tags/tags/tag) = 0">Not tagged</xsl:when>
            <xsl:otherwise>
              Tagged:
              <xsl:for-each select="$tags/tags/tag">
                <a href="{umbraco.library:NiceUrl($currentBlog/@id)}?filterby={.}" rel="tag">
                  <xsl:value-of select="."/>
                </a>
                <xsl:if test="position() != last()">, </xsl:if>
              </xsl:for-each>
            </xsl:otherwise>
          </xsl:choose>
        </span>
        <span class="meta-sep"> | </span>
        <xsl:variable name="numberofcomments" select="count($post//*[(@isDoc or name()='node') and BlogLibrary:IsType('Comment',@id)])"/>
        <span class="comments-link">
          <xsl:choose>
            <xsl:when test="$numberofcomments = 0">
              <xsl:choose>
                <xsl:when test="string($post/closeComments) = '1' or string($currentBlog/disableComments) = '1'">Comments closed</xsl:when>
                <xsl:otherwise>
                  <a href="{umbraco.library:NiceUrl(@id)}#comments">Leave a comment</a>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:when test="$numberofcomments = 1">
              <a href="{umbraco.library:NiceUrl(@id)}#comments">1 comment</a>
            </xsl:when>
            <xsl:otherwise>
              <a href="{umbraco.library:NiceUrl(@id)}#comments">
                <xsl:value-of select="$numberofcomments"/> comments
              </a>
            </xsl:otherwise>
          </xsl:choose>
        </span>
      </footer>
    </article>
  </xsl:template>

  <!-- Nav Bar template - Using HTML5 elements - if you want to use older tags, replace "nav" with "div" -->

  <xsl:template name="navBar">
    <xsl:param name="position"/>
    <nav id="pager-{$position}" class="pager">
      <ul>
        <xsl:if test="$pageNumber &gt; 1">
          <li class="previous">
            <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber - 1}&amp;filterby={$filter}" title="Previous Page">previous</a>
          </li>
        </xsl:if>
        <xsl:call-template name="pager">
          <xsl:with-param name="i">1</xsl:with-param>
          <xsl:with-param name="page" select="$pageNumber"></xsl:with-param>
          <xsl:with-param name="count" select="ceiling($numberOfRecords div $pageSize)"></xsl:with-param>
        </xsl:call-template>
        <xsl:if test="(($pageNumber) * $pageSize) &lt; ($numberOfRecords)">
          <li class="next">
            <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$pageNumber + 1}&amp;filterby={$filter}" title="Next Page">next</a>
          </li>
        </xsl:if>
      </ul>
    </nav>
  </xsl:template>

  <xsl:template name="pager">
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:param name="page"/>
    <xsl:if test="$i &lt;= $count">
      <li class="page">
        <xsl:if test="$page != $i">
          <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i}&amp;filterby={$filter}" title="Go to page {$i} of {$count}" >
            <xsl:value-of select="$i" />
          </a>
        </xsl:if>
        <xsl:if test="$page = $i">
          <span class="currentPage">
            <xsl:value-of select="$i" />
          </span>
        </xsl:if>
      </li>
    </xsl:if>
    <xsl:if test="$i &lt;= $count">
      <xsl:call-template name="pager">
        <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
        </xsl:with-param>
        <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
        </xsl:with-param>
        <xsl:with-param name="page">
          <xsl:value-of select="$page"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>